package com.yic.module.qms.controller.admin.inspectentry;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.base.api.factory.FactoryApi;
import com.yic.module.base.api.factory.dto.FactoryRespDTO;
import org.mapstruct.ap.shaded.freemarker.cache.FileTemplateLoader;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.qms.controller.admin.inspectentry.vo.*;
import com.yic.module.qms.dal.dataobject.inspectentry.InspectEntryDO;
import com.yic.module.qms.convert.inspectentry.InspectEntryConvert;
import com.yic.module.qms.service.inspectentry.InspectEntryService;

@Tag(name = "管理后台 - 检验项目")
@RestController
@RequestMapping("/qms/inspect-entry")
@Validated
public class InspectEntryController {

    @Resource
    private InspectEntryService inspectEntryService;
    @Resource
    private FactoryApi factoryApi;

    @PostMapping("/create")
    @Operation(summary = "创建检验项目")
    @PreAuthorize("@ss.hasPermission('qms:inspect-entry:create')")
    public CommonResult<Long> createInspectEntry(@Valid @RequestBody InspectEntryCreateReqVO createReqVO) {
        return success(inspectEntryService.createInspectEntry(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新检验项目")
    @PreAuthorize("@ss.hasPermission('qms:inspect-entry:update')")
    public CommonResult<Boolean> updateInspectEntry(@Valid @RequestBody InspectEntryUpdateReqVO updateReqVO) {
        inspectEntryService.updateInspectEntry(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除检验项目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:inspect-entry:delete')")
    public CommonResult<Boolean> deleteInspectEntry(@RequestParam("id") Long id) {
        inspectEntryService.deleteInspectEntry(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得检验项目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:inspect-entry:query')")
    public CommonResult<InspectEntryRespVO> getInspectEntry(@RequestParam("id") Long id) {
        InspectEntryDO inspectEntry = inspectEntryService.getInspectEntry(id);
        return success(InspectEntryConvert.INSTANCE.convert(inspectEntry));
    }

    @GetMapping("/list")
    @Operation(summary = "获得检验项目列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('qms:inspect-entry:query')")
    public CommonResult<List<InspectEntryRespVO>> getInspectEntryList(@RequestParam("ids") Collection<Long> ids) {
        List<InspectEntryDO> list = inspectEntryService.getInspectEntryList(ids);
        return success(InspectEntryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得检验项目分页")
    @PreAuthorize("@ss.hasPermission('qms:inspect-entry:query')")
    public CommonResult<PageResult<InspectEntryRespVO>> getInspectEntryPage(@Valid InspectEntryPageReqVO pageVO) {
        PageResult<InspectEntryDO> pageResult = inspectEntryService.getInspectEntryPage(pageVO);
        PageResult<InspectEntryRespVO> resp = InspectEntryConvert.INSTANCE.convertPage(pageResult);
        resp.getList().forEach(item -> {
            FactoryRespDTO factory = factoryApi.getFactory(item.getFactoryId());
            if (ObjectUtil.isNotEmpty(factory)) {
                item.setFactoryName(factory.getName());
            }
        });
        return success(resp);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出检验项目 Excel")
    @PreAuthorize("@ss.hasPermission('qms:inspect-entry:export')")
    @OperateLog(type = EXPORT)
    public void exportInspectEntryExcel(@Valid InspectEntryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InspectEntryDO> list = inspectEntryService.getInspectEntryList(exportReqVO);
        // 导出 Excel
        List<InspectEntryExcelVO> datas = InspectEntryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "检验项目.xls", "数据", InspectEntryExcelVO.class, datas);
    }

}
